package com.ruoyi.project.agreement.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.agreement.domain.AgreementInfo;
import com.ruoyi.project.agreement.mapper.AgreementInfoMapper;
import com.ruoyi.project.agreement.service.IAgreementInfoService;
import com.ruoyi.project.common.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class AgreementInfoServiceImpl implements IAgreementInfoService {

    @Autowired
    private AgreementInfoMapper agreementMapper;

    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<AgreementInfo> selectAgreementList(AgreementInfo agreementInfo) {
        if(agreementInfo.getEndDate() != null) {
            agreementInfo.setEndDate(DateUtils.addDays(agreementInfo.getEndDate(), 1));
        }

        return agreementMapper.selectAgreementList(agreementInfo);
    }

    @Override
    public List<AgreementInfo> selectAgreementByCustomId(String customId) {
        return agreementMapper.selectAgreementByCustomId(customId);
    }

    @Override
    public List<AgreementInfo> selectValidAgreementList(AgreementInfo agreementInfo) {
        return agreementMapper.selectValidAgreementList(agreementInfo);
    }

    @Override
    public AgreementInfo selectAgreementById(String id) {
        return agreementMapper.selectAgreementById(id);
    }

    @Override
    public AjaxResult insertAgreement(AgreementInfo agreementInfo) {
        agreementInfo.setId(SnowflakeIdWorker.generateId().toString());
        agreementInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        agreementInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        agreementInfo.setStatus("0");

        int i = agreementMapper.insertAgreement(agreementInfo);
        if(i > 0) {
            return AjaxResult.success("新增合同成功！");
        } else {
            return AjaxResult.error("新增合同失败！");
        }
    }

    @Override
    public AjaxResult updateAgreement(AgreementInfo agreementInfo) {
        agreementInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());

        int i = agreementMapper.updateAgreement(agreementInfo);
        if(i > 0) {
            return AjaxResult.success("修改合同成功！");
        } else {
            return AjaxResult.error("修改合同失败！");
        }
    }

    @Override
    public int deleteAgreement(String id) {
        return agreementMapper.deleteAgreement(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteAgreements(String[] ids) {
        int count = 0;
        for(int i = 0; i < ids.length; i++) {
            count += agreementMapper.deleteAgreement(ids[i]);
        }
        return count;
    }

    @Override
    public List<AgreementInfo> selectMaxEndDateAgreement(AgreementInfo agreementInfo) {
        if(agreementInfo.getEndDate2() != null) {
            agreementInfo.setEndDate2(DateUtils.addDays(agreementInfo.getEndDate2(), 1));
        }

        return agreementMapper.selectMaxEndDateAgreement(agreementInfo);
    }
}
